﻿using Elsa.Events;
using MediatR;
using Zhp.Workflow.Models;
using Zhp.Workflow.Notify.Services;

namespace Zhp.Workflow.Notify
{
    /// <summary>
    /// 流程到审批节点会先被挂起，然后通知审批人去审批
    /// </summary>
    public class WorkflowSuspendedHander : INotificationHandler<WorkflowSuspended>
    {
        private readonly ITodoService _todoService;

        public WorkflowSuspendedHander(ITodoService todoService)
        {
            _todoService = todoService;
        }

        /// <summary>
        /// 流程到审批节点会先被挂起，然后通知审批人去审批
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task Handle(WorkflowSuspended notification, CancellationToken cancellationToken)
        {
            var wf = notification.WorkflowExecutionContext.WorkflowInstance;
            if (notification.WorkflowExecutionContext.HasBlockingActivities)
            {
                await _todoService.InsertTodo(wf, WorkflowStatusEnum.InProgress);
            }
        }
    }
}
